package com.bikes.mileagetracker.utils

import android.location.Address
import android.location.Geocoder
import android.location.Location
import android.util.Log
import com.bikes.mileagetracker.MileageTrackerApplication
import java.util.*

class AddressUtils {

    companion object {
        @JvmStatic
        fun recoAddress(loc: Location):String?{
            try {
                val geocoder = Geocoder(MileageTrackerApplication.getCurrentApplication(), Locale.getDefault())
                val addresses = geocoder.getFromLocation(loc.latitude, loc.longitude, 1)
                val address = addresses?.firstOrNull()
                if (address is Address) {
                    val add = StringBuilder()
                    for (i in 0..address.maxAddressLineIndex) {
                        add.append(address.getAddressLine(i))
                    }
                    return add.toString()
                }
            }catch (th:Throwable){
                Log.v("console", th.localizedMessage)
            }
            return null
        }
    }

}